package edu.colorado.karl.login;

import java.util.LinkedList;

import edu.colorado.karl.databases.UserDatabase;

/**
 * This class handles all account management tasks.
 * It is used to create, delete and modify user accounts.
 * It reads and writes the 'user_data' table in the Users Database.
 * @author Addison LeMessurier
 * @version 4/14/08
 */

public class AccountManager {
	private static boolean addNew;		// tells whether a new user is being created
	private static AccountManager ref;	// only allowable reference to this class
	private static UserDatabase UDB;

	/**
	 * Adds a new user to the user_data table.
	 * @param username The name of the user.
	 * @param isAdmin <code>true</code> if the user should have
	 * administrative privileges, <code>false</code> otherwise.
	 */
	public static void addUser(String username, boolean isAdmin) {
		UDB.addUser(username, isAdmin);
	}

	/**
	 * This function instantiates the singleton object for this class.
	 * @return a globally-shared instance of AccountManager.
	 */
	public static AccountManager getAccountManager() {
		if (ref == null) {
			ref = new AccountManager();
		}

		return ref;
	}

	 /**
	  * This function checks whether a new user is currently being added.
	  * @return <code>true</code> if a new user is currently being added.
	  */
	public static boolean getAddNew() {
		return addNew;
	}

	/**
	 * This function returns a LinkedList containing all usernames.
	 * @return a list of all usernames.
	 */
	public static LinkedList<String> getUsers() {
		LinkedList<String> users = UserDatabase.getUsers();
		
		return users;
	}

	/**
	 * This function changes a user's access level to 2 (administrator).
	 * @param username The user to modify.
	 */
	public static void grantAdminAccess(String username) {
		UDB.grantAdminAccess(username);
	}

	/**
	 * This function removes a user with the given name from the 'user_data'
	 * table.
	 * @param username The user to remove.
	 */
	public static void removeUser(String username) {
		UDB.removeUser(username);
	}

	/**
	 * This function changes a user's access level to 1 (residential user).
	 * @param username The user to modify.
	 */
	public static void revokeAdminAccess(String username) {
		UDB.revokeAdminAccess(username);
	}

	/**
	 * This function tells the page whether to add a new row in the accounts
	 * table.
	 * @param value <code>true</code> if a new row should be added to the table.
	 */
	public static void setAddNew(boolean value) {
		addNew = value;
	}
	
	/**
	 * Sole constructor
	 */
	private AccountManager() {
		UDB = new UserDatabase();
	}
}
